<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

<html>
  <head>
    <link href="css/style.css" type="text/css" rel="stylesheet">
    <script type="text/javascript" src="bin/prototype.js"></script>
    <script type="text/javascript" src="bin/functions.js"></script>


    <title> LYON UNO </title>
  </head>

  <body>

    <div id="head_pic">
      <img src="images/title.png" alt="[LYON UNO]" id="picture">
    </div>

    <div id="webpage">
<?php
	$gameName = $_GET['game'];

	echo "<h2> You are in the game ".$gameName." waiting for the players</h2>";

	$hostDB = "localhost";
	$userDB = "ampersand";
	$passDB = "palomitas";
	$dbnameDB = "LYONUNO";

	$canalDB = mysql_connect($hostDB, $userDB, $passDB);
	$db = mysql_select_db($dbnameDB);
	$consulta = 'select * from GAME where name=\''.$gameName.'\'';

	$resDB = mysql_db_query($dbnameDB, $consulta,$canalDB);

	echo "<table border = 2>";
	echo "<tr><td><h3>Player</h3></td><td><h3>Logo</h3></td></tr>";
	$row = mysql_fetch_object($resDB);
	$pl1 = $row->creator;
	$pl2 = $row->nick1;
	$pl3 = $row->nick2;
	$pl4 = $row->nick3;
	$p2in = $row->nick1_in;
	$p3in = $row->nick2_in;
	$p4in = $row->nick3_in;
	$pub = $row->public_game;
	mysql_free_result($resDB);

	$newPlayer = $_COOKIE["loggedIn"];

	if($pub == 'y'){
		if($p2in == 'n' && $newPlayer != $pl1){
			$consulta = 'update GAME set nick1 =\''.$newPlayer.'\', nick1_in=\'y\' where name = \''.$gameName.'\'';
			$pl2 = $newPlayer;
			mysql_db_query($dbnameDB, $consulta,$canalDB);
		}else if($p3in == 'n' && $newPlayer != $pl1 && $newPlayer != $pl2){
			$consulta = 'update GAME set nick2 =\''.$newPlayer.'\', nick2_in=\'y\' where name = \''.$gameName.'\'';
			$pl3 = $newPlayer;
			mysql_db_query($dbnameDB, $consulta,$canalDB);
		}else if($p4in == 'n' && $newPlayer != $pl1 && $newPlayer != $pl2 && $newPlayer != $pl3){
			$consulta = 'update GAME set nick3 =\''.$newPlayer.'\', nick3_in=\'y\' where name = \''.$gameName.'\'';
			$pl4 = $newPlayer;
			mysql_db_query($dbnameDB, $consulta,$canalDB);
			$p4in = 'y';
		}
	}
	else{
		if($pl2 == $newPlayer && $p2in =='n'){
			$consulta = 'update GAME set nick1_in=\'y\' where name = \''.$gameName.'\'';
			mysql_db_query($dbnameDB, $consulta,$canalDB);
		}else if($pl3 == $newPlayer && $p3in =='n'){
			$consulta = 'update GAME set nick2_in=\'y\' where name = \''.$gameName.'\'';
			mysql_db_query($dbnameDB, $consulta,$canalDB);
		}else if($pl4 == $newPlayer && $p4in =='n'){
			$consulta = 'update GAME set nick3_in=\'y\' where name = \''.$gameName.'\'';
			mysql_db_query($dbnameDB, $consulta,$canalDB);
			$p4in = 'y';
		}				
	}

	$consulta = 'select pict_URL from USERS where nick=\''. $pl1 .'\'';
	$resDB = mysql_db_query($dbnameDB, $consulta,$canalDB);
	$row = mysql_fetch_object($resDB);
	$pic1 = $row->pict_URL;
	mysql_free_result($resDB);

	$consulta = 'select pict_URL from USERS where nick=\''. $pl2 .'\'';
	$resDB = mysql_db_query($dbnameDB, $consulta,$canalDB);
	$row = mysql_fetch_object($resDB);
	$pic2 = $row->pict_URL;
	mysql_free_result($resDB);

	$consulta = 'select pict_URL from USERS where nick=\''. $pl3 .'\'';
	$resDB = mysql_db_query($dbnameDB, $consulta,$canalDB);
	$row = mysql_fetch_object($resDB);
	$pic3 = $row->pict_URL;
	mysql_free_result($resDB);

	$consulta = 'select pict_URL from USERS where nick=\''. $pl4 .'\'';
	$resDB = mysql_db_query($dbnameDB, $consulta,$canalDB);
	$row = mysql_fetch_object($resDB);
	$pic4 = $row->pict_URL;
	mysql_free_result($resDB);

	if($pic1 == '')
		$pic1 = "http://elpilar2007.com/wp-content/uploads/2007/10/070430marianico.jpg";
	if($pic2 == '')
		$pic2 = "http://elpilar2007.com/wp-content/uploads/2007/10/070430marianico.jpg";
	if($pic3 == '')
		$pic3 = "http://elpilar2007.com/wp-content/uploads/2007/10/070430marianico.jpg";
	if($pic4 == '')
		$pic4 = "http://elpilar2007.com/wp-content/uploads/2007/10/070430marianico.jpg";

	echo "<tr><td><b>" . $pl1 ."</b></td><td><img src=\"". $pic1 ."\" width=60 height=60></img></td></tr>";
	echo "<tr><td><b>" . $pl2 ."</b></td><td><img src=\"". $pic2 ."\" width=60 height=60></img></td></tr>";
	echo "<tr><td><b>" . $pl3 ."</b></td><td><img src=\"". $pic3 ."\" width=60 height=60></img></td></tr>";
	echo "<tr><td><b>" . $pl4 ."</b></td><td><img src=\"". $pic4 ."\" width=60 height=60></img></td></tr>";
	echo "</table>";

	mysql_db_query($dbnameDB, "commit",$canalDB);
	mysql_close($canalDB);

	if($pl1 == $newPlayer)
		$actualPlayer = 1;
	else if($pl2 == $newPlayer)
		$actualPlayer = 2;
	else if($pl3 == $newPlayer)
		$actualPlayer = 3;
	else if($pl4 == $newPlayer)
		$actualPlayer = 4;
	$game = $gameName;

	if($p2in == 'y' && $p3in == 'y' && $p4in == 'y'){
		echo "<br><br><br>";
		echo "\n<form method = \"POST\" action=\"http://www.marca.com\">";
		echo "<input type=\"hidden\" name=\"pl1\" value=\"$pl1\" />";
		echo "<input type=\"hidden\" name=\"pl2\" value=\"$pl2\" />";
		echo "<input type=\"hidden\" name=\"pl3\" value=\"$pl3\" />";
		echo "<input type=\"hidden\" name=\"pl4\" value=\"$pl4\" />";
		echo "<input type=\"hidden\" name=\"pic1\" value=\"$pic1\" />";
		echo "<input type=\"hidden\" name=\"pic2\" value=\"$pic2\" />";
		echo "<input type=\"hidden\" name=\"pic3\" value=\"$pic3\" />";
		echo "<input type=\"hidden\" name=\"pic4\" value=\"$pic4\" />";
		echo "<input type=\"hidden\" name=\"actualPlayer\" value=\"$actualPlayer\" />";
		echo "<input type=\"hidden\" name=\"game\" value=\"$game\" />";
		echo "<input type=\"submit\" name=\"send\" value=\"All in!\"/>";
		echo "</form>";
	}else

?>

    </div>

    <div id="foot">
      <address>LYON UNO. CPE <br> 43, boulevard du 11 Novembre 1918. BP 2077 <br> 69616 Villeurbanne Cedex </address>
    </div>
  </body>
</html>
